//  Created by <Omar MALAGOUEN>

//  Copyright 2011 All rights reserved.
//

#include "cnewprofile.h"
#include "ui_cnewprofile.h"

CNewProfile::CNewProfile(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::CNewProfile)
{
    ui->setupUi(this);
    accepted = false;
}

CNewProfile::~CNewProfile()
{
    delete ui;
}

void CNewProfile::setfileName(QString filename)
{
    ui->lineEdit_filename->setText(QDir::cleanPath(filename));
    this->fileName = QDir::cleanPath(filename);
}


void CNewProfile::setdirName(QString dirname)
{
    ui->lineEdit_dirname->setText(QDir::cleanPath(dirname));
    ui->lineEdit_dirname->setDisabled(true);
    this->dirName = QDir::cleanPath(dirname);
}


void CNewProfile::setcontent(QStringList flux)
{
    QString s = flux.join("\r\n");
    ui->textEdit->setText(s);
}

void CNewProfile::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void CNewProfile::on_buttonBox_accepted()
{
    dirName = QDir::cleanPath(ui->lineEdit_dirname->text());
    fileName = QDir::cleanPath(ui->lineEdit_filename->text());
    QString completeFileName =  QDir::cleanPath(dirName + "/" + fileName);

    QFile *fichier = new QFile(completeFileName);
    fichier->open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream flux(fichier);
    flux << ui->textEdit->toPlainText();
    this->content = ui->textEdit->toPlainText().split("\r\n");
    fichier->close();

    accepted = true;
    delete fichier;
}

void CNewProfile::on_buttonBox_rejected()
{
    qDebug("New profile creation cancelled");
    accepted = false;
}
